#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "modifyinfo.h"
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mSettings=new QSettings(QDir::toNativeSeparators(QDir::currentPath()+"/data.ini"),QSettings::IniFormat);
    QAction* modifyAction=ui->toolBar->addAction(QIcon(":/modify"),"修改信息");
    connect(modifyAction,SIGNAL(triggered()),this,SLOT(onModifyActionTriggered()));

    readSettings();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readSettings()
{
   QStringList list=mSettings->value("person").toStringList();
   ui->tblSchedule->setColumnCount(list.count());
   ui->tblSchedule->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
   list=mSettings->value("time").toStringList();
   ui->tblSchedule->setRowCount(list.count());
   ui->tblSchedule->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
}


void MainWindow::removeRedundanceSettings()
{

    QStringList list=mSettings->value("person").toStringList();
    int n=list.count();
    int *flag=new int[n];
    for(int i=0;i<n;i++){
        flag[i]=0;
    }
    int cnt=0;
    for(QString s:list){
        if(!mSettings->contains("person:"+s)){
            flag[cnt]=1;
        }
        cnt++;
    }
    cnt=0;
    for(int i=0;i<n;i++){
        if(flag[i]==1)
        {
            list.removeAt(i-cnt);
            cnt++;
        }
    }
    delete[] flag;
    list=mSettings->value("time").toStringList();
    n=list.count();
    flag=new int[n];
    for(int i=0;i<n;i++){
        flag[i]=0;
    }
    cnt=0;
    for(QString s:list){
        if(!mSettings->contains("time:"+s)){
            flag[cnt]=1;
        }
        cnt++;
    }
    cnt=0;
    for(int i=0;i<n;i++){
        if(flag[i]==1)
        {
            list.removeAt(i-cnt);
            cnt++;
        }
    }
    delete[] flag;
}

void MainWindow::onModifyActionTriggered()
{

    ModifyInfo dialog(mSettings,this);
    dialog.exec();
    removeRedundanceSettings();
    readSettings();

}
